package com.cs.rabbitmq.receive;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author shuai
 * @date 2019/5/21
 */
@Component
public class Receiver {

    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = "hello")
    @RabbitHandler
    public void process(String msg) {
        System.out.println("Receiver:"+msg);
    }

    @RabbitListener(queues = "queue", containerFactory = "singleListenerContainer")
    public void consumeUserLogQueue(@Payload byte[] message){
        try {
            String string=objectMapper.readValue(message, String.class);
            System.out.println("监听消费用户日志 监听到消息:" + string);
            //TODO：记录日志入数据表
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
